import { defineStore } from 'pinia'
import { reqC1, reqC2, reqC3 } from '@/api/product/attr'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { ResponseCategoryData, CategoryObj } from '@/api/product/attr/type'
const useCategoryStore = defineStore('Category', () => {
  let c1Arr = ref<CategoryObj[]>([])
  let c1Id = ref<number | string>('')
  const getC1 = () => {
    reqC1().then((res: ResponseCategoryData) => {
      if (res.code == 200) {
        c1Arr.value = res.data
      } else {
        ElMessage({
          type: 'error',
          message: '获取一级分类失败!',
        })
      }
    })
  }
  let c2Arr = ref<CategoryObj[]>([])
  let c2Id = ref<number | string>('')
  const getC2 = () => {
    reqC2(c1Id.value).then((res: ResponseCategoryData) => {
      if (res.code == 200) {
        c2Arr.value = res.data
      } else {
        ElMessage({
          type: 'error',
          message: '获取二级分类失败!',
        })
      }
    })
  }
  let c3Arr = ref<CategoryObj[]>([])
  let c3Id = ref<number | string>('')
  const getC3 = () => {
    reqC3(c2Id.value).then((res: ResponseCategoryData) => {
      if (res.code == 200) {
        c3Arr.value = res.data
      } else {
        ElMessage({
          type: 'error',
          message: '获取三级分类失败!',
        })
      }
    })
  }
  return { getC1, c1Arr, c1Id, getC2, c2Id, c2Arr, getC3, c3Id, c3Arr }
})

export default useCategoryStore
